package com.ocl.www.service;

import java.util.ArrayList;
import java.util.List;

import com.ocl.www.config.TargetDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ocl.www.domain.Product;
import match.MatchingResult;
import com.ocl.www.mapper.ProductMapper;

@Service
public class ProductService {

	@Autowired
	private ProductMapper productMapper;

	/**
	 * 
	 * @Title: ProductService
	 * @Description: 获取胶囊产品信息
	 * @param: @param matchResult
	 * @param: @return
	 * @param: @throws Exception
	 * @return: List<Product>
	 * @throws
	 */
	@TargetDataSource(name = "clmp")
	public List<Product> findByMainPicture(MatchingResult[] matchResult) throws Exception {
		try {
			List<Product> products = new ArrayList<Product>();
			/*if(null == products || products.size() <1){
				return null;
			}*/
			for (MatchingResult mResult : matchResult) {
				if (null == mResult){
					continue;
				}
				//是带路径的文件名
				String mainPicture = mResult.getmStrName();
				System.out.println("mainPicture="+mainPicture);
				String[] paths = mainPicture.split("/");
				mainPicture = paths[paths.length-1];
				//去掉后缀名
				String [] names = mainPicture.split("\\.");
				mainPicture = names[0].trim();
				System.out.println("mainPicture2="+mainPicture);
				//取出文件名后查询
				Product product = productMapper.selectByMainPicture(mainPicture);
				if(null != product){
					//产品不为空则给"分类"字段赋值
					product.setCategory(mResult.getmNSimilary());
					products.add(product);
				}
			}
			return products;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	
}
